<?php
class ChapterDao {
	public $table = 'chapter';
	public function get($id){
		return ClassManager::getSql()->onedata($this->table,'*',"where id =".intval($id));
	}
	
	public function add($data){
		ClassManager::getSql()->insert($this->table, $data);
	}
	
	public function update($data,$id){
		ClassManager::getSql()->updates($this->table, $data,"where id=".intval($id));
	}
	
	public function searchList($where=array(),$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	
	
	public function searchPairList($where=array(),$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'id,name,english_name',$where,$order,$limit);
	}
	
	public function searchNumber($where){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"count(*)  ",$where);
		return $tmp[0];
	}
	
	public function searchWhere($where){
		$where1 = "where 1=1 ";
		$english_tag = array('name','content');
		$likeWhere = array('name','content');
		$fieldArray =  ClassManager::getTableConfig()->getFieldArray($this->table);
		foreach ($where as  $k=>$v){
			if (!empty($v)){
				if (in_array($k,$likeWhere) ){
					if(in_array($k,$english_tag)){
						$where1 .= "and  (`".$k."`  like '%".$v."%'";
						$where1 .= " or ";
						$where1 .= " `".TAB_ENGLISH_FLAG.$k."`  like '%".$v."%' )";
					}else{
						$where1 .= "and  `".$k."`  like '%".$v."%'";
					}
				}
				else{
					if(in_array($k,$fieldArray)){
						$where1 .= "and  `".$k."`  = '".$v."'";
					}
				}
			}
		}
		return $where1;
	}
	
	public function deleteBatchByid($id){
		ClassManager::getSql()->delete($this->table,"where id in(".$id.")");
	}
	public function getListByCourseId($courseId){
		return ClassManager::getSql()->datalist($this->table,'*',"where course_id='".intval($courseId)."'");
	}
	public function getListByCourseIdList($courseIdList,$chapter_no){
		return ClassManager::getSql()->datalist($this->table,'*',"where course_id in (".$courseIdList.") and chater_no='".$chapter_no."'");
	}
}

?>